!
! MODPAR  Commands get by a command file.
!

Module Param

  integer, parameter, private :: MAXCOM = 10
  integer, parameter, private :: LENCOM = 80
  integer, private :: ncom          ! number of current parameters 
  character(len=LENCOM), private :: com(MAXCOM)

contains

!------------------------------------------------------------------------

  subroutine nextcom(file, istat)
    
    !
    ! subrutine read next line from command file
    !
    character(len=*) :: file
    character(len=LENCOM) :: line
    integer :: istat, i, left, right
    logical, save :: open = .false.
    
    if( .not. open )then
       open(15,file=file,status='old',err=666)
       open = .true. 
    endif
    read(15,'(a)',err=666,end=20) line

    !
    ! this part reads commands (lines) from a previously opened
    ! file and it splita lines to simple strings
    !
    ncom = 0
    right = 0
    left = 0
    do i = 1, LENCOM
       ! we assume that ' ' or ',' for parameter's separator
       ! this code do not accept parameter ended by 80th 
       ! non- separator character 
       if(line(i:i) == ' ' .or. line(i:i) == ',') then
          if( right /= 0 )then
             if( ncom < MAXCOM )then
                ncom = ncom + 1
             else
                exit
             endif
             com(ncom) = line(left+1:right)
             right = 0
          endif
          left = i
       else
          right = i
       endif
!       write (*,*) i,left,right,trim(line)
    enddo
    
    istat = 0
    return
    
666 istat = 110
    write(*,*) "Command file can't be read."
    return
    
20  istat = -1
    close(15)
    open = .false.
    return
    
  end subroutine
  
!----------------------------------------------------------------------

  function iargcom()

    integer :: iargcom

    iargcom = ncom

  end function iargcom

!-----------------------------------------------------------------------

  subroutine getcom( icom, scom )

    integer :: icom
    character(len=*) :: scom

    if( 0 < icom .and. icom <= ncom ) then
       scom = com(icom)
    else
       scom = " "
    endif

  end subroutine getcom

end module
